package com.sinoeyes.controller.system;

import com.sinoeyes.common.config.PerformanceConfig;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.common.utils.file.FileUploadUtils;
import com.sinoeyes.framework.config.ServerConfig;
import com.sinoeyes.vo.system.UploadFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求处理
 */
@RestController
@Api(value = "通用请求处理 API", tags = {"通用请求处理"})
public class CommonController
{
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    @ApiOperation("上传文件")
    public Result<UploadFileVo> uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = PerformanceConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            UploadFileVo uploadFileVo = new UploadFileVo();
            uploadFileVo.setFileName(fileName);
            uploadFileVo.setUrl(url);
            return Result.success(uploadFileVo);
        }
        catch (Exception e)
        {
            return Result.failure(RCode.FAILURE);
        }
    }
}
